package com.ciaojian.store.controller.franchisees;

import cn.hutool.core.util.ObjectUtil;
import com.ciaojian.core.mapper.OrderCommentMapper;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.OrderComment;
import com.ciaojian.core.model.User;
import com.ciaojian.core.model.vo.OrderCommentVo;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

@Api(tags = "加盟商订单接口")
@RestController
@RequestMapping("/franchisees/order")
public class FOrderController {

    @Resource
    private OrderCommentMapper orderCommentMapper;

    @ApiOperation("订单详情")
    @GetMapping("/orderComment/{id}")
    public Result workerComment(@PathVariable Integer id) {

        OrderComment orderComment = orderCommentMapper.selectById(id);

        Order order = new Order().selectById(orderComment.getOrderId());

        User user = new User().selectById(order.getUserId());


        OrderCommentVo orderCommentVo = new OrderCommentVo();

        orderCommentVo.setWorkerId(order.getWorkerId());
        orderCommentVo.setWorkerName(order.getWorkerName());
        orderCommentVo.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:ss:mm").format(order.getAddTime()));
        orderCommentVo.setPayTotal(order.getPayTotal());
        orderCommentVo.setTotal(order.getTotal());
        orderCommentVo.setType(order.getType());
        orderCommentVo.setOrderNo(order.getOrderNo());
        orderCommentVo.setScore(orderComment.getScore());
        orderCommentVo.setId(orderComment.getId());
        orderCommentVo.setUserId(user.getId());
        if (ObjectUtil.isNotNull(user.getNickName())) {
            orderCommentVo.setUserNickname(user.getNickName());
        }else {
            orderCommentVo.setUserNickname(user.getDyNickName());
        }
        orderCommentVo.setUserPhone(user.getTelphone());
        if (orderComment.getStatus()==1) {
            orderCommentVo.setStatus("已回访");
        }else {
            orderCommentVo.setStatus("未回访");
        }
        orderCommentVo.setTag(orderComment.getTag());
        return Result.ok(orderCommentVo);
    }

    @ApiOperation("回访状态修改")
    @GetMapping("/commentStatus/{id}")
    public Result commentStatus(@PathVariable Integer id) {

        OrderComment orderComment = orderCommentMapper.selectById(id);

        if (orderComment.getStatus()==0){
            orderComment.setStatus(1);
        }

        if (orderComment.getStatus()==1){
            orderComment.setStatus(0);
        }
        orderCommentMapper.updateById(orderComment);
        return Result.ok(orderComment);
    }
}
